-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tmpnam.c
69 lines (49 loc) · 1.79 KB
/
Tmpnam.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*++
toro C Library
https://github.com/KilianKegel/toro-C-Library#toro-c-library-formerly-known-as-torito-c-library
Copyright (c) 2017-2024, Kilian Kegel. All rights reserved.
SPDX-License-Identifier: GNU General Public License v3.0
Module Name:
Tmpnam.c
Abstract:
Implementation of the Standard C function.
Generate names you can use to create temporary files.
Author:
Kilian Kegel
--*/
#include <CdeServices.h>
#include <string.h>
extern char* _ltoa(long value, char* str, int base);
/** tmpnam
Synopsis
#include <stdio.h>
char *tmpnam(char *s);
Description
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tempnam-wtempnam-tmpnam-wtmpnam?view=msvc-160&viewFallbackFrom=vs-2019
The tmpnam function generates a string that is a valid file name and that is not the same
as the name of an existing file. The function is potentially capable of generating
TMP_MAX different strings, but any or all of them may already be in use by existing files
and thus not be suitable return values.
**/
char* tmpnam(char* s)
{
CDE_APP_IF* pCdeAppIf = __cdeGetAppIf();
char ext[16] = { "." };
long l = (long)(0xFFFFFFF & pCdeAppIf->pCdeServices->pGetTsc(pCdeAppIf));
pCdeAppIf->szTmpBuf[0] = 's'; // MSFT names begin with "s"
_ltoa(pCdeAppIf->lTmpNamNum, &ext[1], 36); // generate number extension
if (0 == pCdeAppIf->lTmpNamNum++)
{
_ltoa(l, &pCdeAppIf->szTmpBuf[1], 36); // append number name after s
}
else
{
*(strchr(pCdeAppIf->szTmpBuf, '.')) = '\0'; // kill "."
}
strcat(pCdeAppIf->szTmpBuf, ext); // append extension
if (NULL != s)
{
strcpy(s, pCdeAppIf->szTmpBuf);
}
return NULL == s ? &pCdeAppIf->szTmpBuf[0] : s;
}